from django.shortcuts import render
from apps.disclose.models import Spit
from rest_framework.generics import ListAPIView,UpdateAPIView,CreateAPIView,RetrieveAPIView,ListCreateAPIView
from rest_framework.viewsets import ModelViewSet
from apps.disclose.serializers import SpitLIstSerializer
from rest_framework.response import Response

# Create your views here.

#吐槽列表,发布吐槽
class SpitListAPIView(ListCreateAPIView):

    def get_queryset(self):
        return Spit.objects.all().order_by('-publishtime')



    def get_serializer_class(self):
        return SpitLIstSerializer

    def create(self, request, *args, **kwargs):
        data = request.data
        # data['user'] = request.user.id
        serializer = SpitLIstSerializer(data=data)
        serializer.is_valid()
        serializer.save()
        # spit.userid = request.user.id
        # spit.save()
        return Response(serializer.data)


#收藏或取消收藏
class SpitCollectAPIView(UpdateAPIView):

    def get_queryset(self):
        id = self.kwargs.get('id')
        return Spit.objects.get(id=id)

    def get_serializer_class(self):
        return SpitLIstSerializer

    def update(self, request, *args, **kwargs):
        id = self.kwargs.get('id')
        spit = Spit.objects.get(id=id)
        if spit.collected:
            spit.collected = False
            spit.save()
            return Response({"message": "取消收藏", "success": True})
        else:
            spit.collected = True
            spit.save()
            return Response({"message": "收藏成功", "success": True})


#点赞或取消点赞
class SpitGiveALikeAPIView(UpdateAPIView):

    def get_queryset(self):
        id = self.kwargs.get('id')
        return Spit.objects.get(id=id)

    def get_serializer_class(self):
        return SpitLIstSerializer

    def update(self, request, *args, **kwargs):
        id = self.kwargs.get('id')
        spit = Spit.objects.get(id=id)
        if spit.hasthumbup:
            spit.hasthumbup = False
            spit.thumbup -= 1
            spit.save()
            return Response({"message": "取消点赞", "success": True})
        else:
            spit.hasthumbup = True
            spit.thumbup += 1
            spit.save()
            return Response({"message": "点赞成功", "success": True})


#吐槽详情
class SpitDetailsListAPIView(RetrieveAPIView):

    def get_object(self):
        id = self.kwargs.get('id')
        return Spit.objects.get(id=id)

    def get_serializer_class(self):
        return SpitLIstSerializer

    def retrieve(self, request, *args, **kwargs):
        spit = self.get_object()
        spit.visits += 1
        spit.save()
        disclose = spit.subs.all()
        disclose.comment_question = []
        for item in disclose:
            disclose.comment_question.append(item)
        s = SpitLIstSerializer(instance=spit)

        return Response(s.data)


#获取吐槽评论
class GETSpitCommentAPIView(ListAPIView):

    def get_queryset(self):
        id = self.kwargs.get('id')
        return Spit.objects.filter(parent_id=id)

    def get_serializer_class(self):
        return SpitLIstSerializer








